﻿// <copyright file="ImageRegionPanel.xaml.cs" company="Taasss">Copyright (c) 2009 All Right Reserved</copyright>
// <author>Ben Vincent</author>
// <date>2010-02-21</date>
// <summary>ImageRegionPanel</summary>
namespace Fotofly.PhotoViewer
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    using Fotofly.PhotoViewer.FotoflyWebService;
    
    public partial class ImageRegionPanel : UserControl
    {
        private int regionBorderSize = 1;
        private int textBoxHeight = 30;
        private int paddingCanvasHeight = 5;

        private double left;
        private double top;

        private MicrosoftImageRegion region;
        private Uri regionUri;
        private bool alwaysShowRegionHotspot;

        public ImageRegionPanel(MicrosoftImageRegion region, double imageWidth, double imageHeight)
        {
            this.InitializeComponent();

            // Update Region data to reflect image size
            this.region = region;
            this.region.Left = (int)Math.Round(region.Left * imageWidth);
            this.region.Top = (int)Math.Round(region.Top * imageHeight);
            this.region.Width = (int)Math.Round(region.Width * imageWidth);
            this.region.Height = (int)Math.Round(region.Height * imageHeight);

            // Set text Box details
            this.nameText.Text = "     " + region.PersonDisplayName + "     ";

            // Set overal Height & Width
            this.Width = this.nameText.ActualWidth;
            this.Height = this.region.Height + this.textBoxHeight + this.paddingCanvasHeight;

            // Set working panel height & width
            this.workingPanel.Width = this.Width;
            this.workingPanel.Height = this.Height;

            // Set regionHotSpot
            this.regionHotSpot.Height = this.region.Height;
            this.regionHotSpot.Width = this.region.Width;

            // Set rectange
            this.regionHotSpotRectangle.Height = this.region.Height - this.regionBorderSize;
            this.regionHotSpotRectangle.Width = this.region.Width - this.regionBorderSize;

            // Set PaddingCanvas
            this.paddingCanvas.Height = this.paddingCanvasHeight;
            this.paddingCanvas.Width = this.Width;

            // Set Label Canvas details
            this.labelCanvas.Width = this.Width;

            // Set Name Canvas details
            this.nameBox.Height = this.textBoxHeight;
            this.nameBox.Width = this.Width;

            // Calculate new Top & Left properties for parent container
            this.left = this.region.Left - ((this.nameText.ActualWidth - this.region.Width) / 2);
            this.top = this.region.Top;

            if (this.left < 0)
            {
                this.left = 0;
            }
        }

        public Uri RegionUri
        {
            get
            {
                return this.regionUri;
            }

            set
            {
                this.regionUri = value;

                if (this.regionUri != null)
                {
                    this.regionHotSpot.Cursor = Cursors.Hand;
                }
            }
        }

        public MicrosoftImageRegion Region
        {
            get
            {
                return this.region;
            }
        }

        public double Top
        {
            get
            {
                return this.top;
            }
        }

        public double Left
        {
            get
            {
                return this.left;
            }
        }

        public bool AlwaysShowRegionHotspot
        {
            set
            {
                this.alwaysShowRegionHotspot = value;
                this.showRegionHotspot = value;
            }
        }

        private bool showRegionHotspot
        {
            set
            {
                if (value)
                {
                    this.paddingCanvas.Visibility = Visibility.Visible;
                    this.labelCanvas.Visibility = Visibility.Visible;
                    this.Opacity = 1;
                }
                else
                {
                    this.paddingCanvas.Visibility = Visibility.Collapsed;
                    this.labelCanvas.Visibility = Visibility.Collapsed;
                    this.Opacity = 0;
                }

                if (this.alwaysShowRegionHotspot)
                {
                    this.regionHotSpotRectangle.StrokeThickness = this.regionBorderSize;
                }
                else
                {
                    this.regionHotSpotRectangle.StrokeThickness = 0;
                }
            }
        }

        private void RegionHotSpot_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!this.alwaysShowRegionHotspot)
            {
                this.showRegionHotspot = false;
            }
        }

        private void RegionHotSpot_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!this.alwaysShowRegionHotspot)
            {
                this.showRegionHotspot = true;
            }
        }

        private void RegionHotSpot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.regionUri != null)
            {
                HtmlPage.Window.Navigate(this.regionUri);
            }
        }
    }
}
